package sealte.mynode.servlet.impl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sealte.mynode.bean.CommentBean;
import sealte.mynode.pojo.Comment;
import sealte.mynode.servlet.system.SystemPostServlet;
import sealte.mynode.util.Tools;

public class CommentAddNew extends SystemPostServlet {

	private static final long serialVersionUID = 6730179237405919133L;

	@Override
	protected void doBusinessService(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		String essayIdStr = req.getParameter("essay_id");
		String userIdStr = req.getParameter("user_id");
		String parentIdStr = req.getParameter("parent_id");
		String content = req.getParameter("content");
		int essayId = Integer.parseInt(essayIdStr);
		int userId = Integer.parseInt(userIdStr);
		int parentId = Integer.parseInt(parentIdStr);
		String dateTime = Tools.getCurrentDate();
		
		Comment com = new Comment();
		com.setContent(Tools.formatContent(content));
		com.setEssayId(essayId);
		com.setModifyTime(dateTime);
		com.setParent(parentId);
		com.setUserId(userId);
		com.setWriteTime(dateTime);
		
		CommentBean cb = new CommentBean();
		boolean flag = cb.addNew(com);
		if(flag){
			resp.sendRedirect("essay.jsp?id="+essayId);
		}else{
			out.println("Comment add new failed!");
			out.println("<a href=\"essay.jsp?id="+essayId+"\">Add new comment again</a>");
		}
		
		if(null!=out){
			out.close();
		}
	}

}
